package com.warehouse.module.business.controller.app.cargoinfo;

import com.google.common.collect.Maps;
import com.warehouse.framework.common.pojo.CommonResult;
import com.warehouse.framework.common.pojo.PageResult;
import com.warehouse.framework.common.util.object.BeanUtils;
import com.warehouse.module.business.controller.admin.cargofile.vo.CargoFileReqVO;
import com.warehouse.module.business.controller.admin.cargoinfo.vo.CargoInfoPageReqVO;
import com.warehouse.module.business.controller.admin.cargoinfo.vo.CargoInfoRespVO;
import com.warehouse.module.business.controller.admin.cargoinfo.vo.CargoInfoSaveReqVO;
import com.warehouse.module.business.dal.dataobject.cargofile.CargoFileDO;
import com.warehouse.module.business.dal.dataobject.cargoinfo.CargoInfoDO;
import com.warehouse.module.business.enums.FileTypes;
import com.warehouse.module.business.service.cargofile.CargoFileService;
import com.warehouse.module.business.service.cargoinfo.CargoInfoService;
import com.warehouse.module.mp.api.MpUserInfoService;
import com.warehouse.module.mp.api.dto.MpUserDTO;
import com.warehouse.module.system.api.appuser.AppUserApi;
import com.warehouse.module.system.api.appuser.dto.AppUserDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.warehouse.framework.common.pojo.CommonResult.success;

@RestController
@RequestMapping("/business/cargo-info")
public class CargoInfoApiController {

    @Resource
    private CargoInfoService cargoInfoService;
    @Resource
    private MpUserInfoService mpUserInfoService;

    @Resource
    private AppUserApi appUserApi;

    @Resource
    private CargoFileService cargoFileService;

    @GetMapping("/page")
    @Operation(summary = "获得货物资料分页")
    public CommonResult<PageResult<CargoInfoRespVO>> getCargoInfoPage(@Valid CargoInfoPageReqVO pageReqVO) {
        AppUserDTO appUserDTO = appUserApi.getAppUserByAppIdOpenId(pageReqVO.getAppId(), pageReqVO.getOpenid());
        pageReqVO.setUserId(appUserDTO.getId());
        PageResult<CargoInfoRespVO> pageResult = cargoInfoService.getCargoInfoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CargoInfoRespVO.class));
    }

    @GetMapping("/get")
    @Operation(summary = "获得货物资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<CargoInfoRespVO> getCargoInfo(@RequestParam("id") Long id) {
        return success(cargoInfoService.getCargoInfo(id));
    }

    @GetMapping("/queryCreateFile/{id}")
    public CommonResult<Map<String, Object>> queryCreateFile(@PathVariable("id") Long id) {
        CargoFileReqVO req1 = new CargoFileReqVO();
        req1.setCargoId(id);
        req1.setFileType(FileTypes.SHENBAO_FILES.getCode());
        List<CargoFileDO> shenbaoFiles = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.ZHIDAN_FILES.getCode());
        List<CargoFileDO> zhidanFiles = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.BAOGUAN_FILES.getCode());
        List<CargoFileDO> baoguanFiles = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.JIANDING_FILES.getCode());
        List<CargoFileDO> jiandingFiles = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.DGD_FILES.getCode());
        List<CargoFileDO> dgdFiles = cargoFileService.getCargoFileList(req1);

        Map<String, Object> result = Maps.newHashMap();
        result.put("shenbaoFiles", shenbaoFiles);
        result.put("zhidanFiles", zhidanFiles);
        result.put("baoguanFiles", baoguanFiles);
        result.put("jiandingFiles", jiandingFiles);
        result.put("dgdFiles", dgdFiles);
        return success(result);
    }

    @GetMapping("/querypack/{id}")
    public CommonResult<Map<String, Object>> packQuery(@PathVariable("id") Long id) {
        CargoFileReqVO req1 = new CargoFileReqVO();
        req1.setCargoId(id);
        req1.setFileType(FileTypes.PACKAGE_GOODSS.getCode());
        List<CargoFileDO> packList = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.RECEIPT_INPUT.getCode());
        List<CargoFileDO> receiptList = cargoFileService.getCargoFileList(req1);

        Map<String, Object> result = Maps.newHashMap();
        result.put("packList", packList);
        result.put("receiptList", receiptList);
        return success(result);
    }

    @GetMapping("/queryshipment/{id}")
    public CommonResult<Map<String, Object>> queryshipment(@PathVariable("id") Long id) {
//        CargoInfoDO cargoInfo = cargoInfoService.getCargoInfo(id);
        CargoFileReqVO req1 = new CargoFileReqVO();
        req1.setCargoId(id);
        req1.setFileType(FileTypes.OUTBOUND.getCode());
        List<CargoFileDO> shipmentList = cargoFileService.getCargoFileList(req1);

        req1.setFileType(FileTypes.WEIGHING_STRIP.getCode());
        List<CargoFileDO> stripList = cargoFileService.getCargoFileList(req1);

        Map<String, Object> result = Maps.newHashMap();
//        result.put("cargoInfo", cargoInfo);
        result.put("shipmentList", shipmentList);
        result.put("stripList", stripList);
        return success(result);
    }



    /** ------------------ 客服/仓库 ---------------- **/
//    @GetMapping("/pagelist")
//    @Operation(summary = "获得货物资料分页")
//    public CommonResult<PageResult<CargoInfoRespVO>> getCargoInfoPageList(@Valid CargoInfoPageReqVO pageReqVO) {
//        PageResult<CargoInfoDO> pageResult = cargoInfoService.getCargoInfoPage(pageReqVO);
//        return success(BeanUtils.toBean(pageResult, CargoInfoRespVO.class));
//    }

    @PostMapping("/create")
    @Operation(summary = "创建货物资料")
    public CommonResult<Long> createCargoInfo(@Valid @RequestBody CargoInfoSaveReqVO createReqVO) {
        return success(cargoInfoService.createCargoInfo(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新货物资料")
    public CommonResult<Boolean> updateCargoInfo(@Valid @RequestBody CargoInfoSaveReqVO updateReqVO) {
        cargoInfoService.updateCargoInfo(updateReqVO);
        return success(true);
    }
}
